use rand::Rng;
use std::alloc::Layout;
use std::cmp::Ordering;
use std::error::Error;
use std::fmt::Debug;
use std::fs::{self, File};
use std::io;
use std::io::BufRead;
use std::io::BufReader;
use std::thread::{self, sleep};
use std::{env, os, path, result, string, time};

const H_PERL: u128 = 19;

pub mod s1;

struct Data<T> {
    x: T,
}

async fn f1() {
    println!("f1...");
}

async fn fstart() {
    println!("fstart...");
    f1().await;
    println!("fend ...");
}

#[test]
fn testasync() {
    println!("------ start ----");
    fstart();
    println!("------ end -----")
}

#[test]
fn do_th() {
    for i in 0..10 {
        println!("{}", i);
        sleep(time::Duration::from_secs(1));
    }
}

#[test]
fn test_time() {
    let th = thread::spawn(do_th);
    println!("aaa");
    sleep(time::Duration::from_secs(1));
    println!("bbbb");
    th.join().unwrap();
}

#[test]
fn test_fw() {
    let mut s = String::from("hello world");

    let s1 = first_word(&mut s);

    println!("{}", s1);
    println!("{}", s)
}

fn main() {
    rust_demo::echo1();
}

fn first_word(s: &mut String) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }

    &s[..]
}

#[cfg(test)]
mod tests {

    const HELLO_NUM: u32 = 16;
    // 变量与可见性
    #[test]
    fn test1() {
        let mut x = 5;
        println!(" x is : {x}, {HELLO_NUM}");
        x = 6;
        println!(" x is :{x}");
    }

    // 数据类型
    #[test]
    fn test2() {
        let a: i8 = 5;
        let b: f32 = 4.5;
        let c: char = 'Z';
        let d = true;
        println!("{a}, {b}, {c}, {d}");

        let a2 = (1, 2.3, true, 'F');
        println!("{:?}", a2);
        println!("{}", a2.1);

        let arr1 = [1, 2, 3];
        let arr2 = vec![1, 2, 3];
        println!("{:?} , {:?}", arr1, arr2);
    }

    // function
    #[test]
    fn test3() {
        let x = 15;
        t3help(x);
        println!("{x}");
    }
    fn t3help(x: i32) -> i64 {
        println!("{x}");
        7
    }

    /*
    #[test]
    fn test_open_file() {
        let f = File::open("/home/jin/code/rust-demo/src/s12.rs");
        match f {
            Ok(ft) => {
                let r = BufReader::new(ft);
                for line in r.lines() {
                    match line {
                        Ok(l) => println!("{}", l),
                        Err(_) => println!("错误22 error"),
                    }
                }
            }
            Err(err) => println!("错误11 {}", err),
        }
    }
    */

    #[test]
    fn test_str() {
        let cc = 18;
        let a: char = 'a';
        let b: String = String::from("b\ni");
        let lines = b.lines();
        for l in lines {
            println!("{}", l)
        }
        println!("{},{}", std::mem::size_of_val(&a), a.len_utf8());
        println!("{}", std::mem::size_of_val(&b))
    }

    #[test]
    fn test_parse() {
        // let args = env::args();
        let mut s = "h";
        println!("{}", s.len());

        let i: u32 = match "33".parse() {
            Ok(num) => num,
            Err(_) => 9,
        };
        println!("{}", i)
    }

    #[test]
    fn testsome() {
        let a = Some(1);
        println!("{:?}", a.unwrap());
        drop(a)
    }

    #[test]
    fn test_p() {
        let s = String::from("hello");
        test_p_help(s);
    }
    fn test_p_help(s: String) {
        println!("{}", s);
    }

    #[test]
    fn test_base() {
        let mut x = 5;
        println!("{}", x);
        x = 6;
        println!("{}", x);
        println!("yy");
    }
}
